import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);

import { setItem, getItem } from "../utils/storage.js";

// 定义一个变量
const TOKEN_KEY = "TOUTIAO_USER";

export default new Vuex.Store({
  //实现vuex仓库模块开发存储数据
  modules: {},
  state: {
    // 存储用户信息
    user: getItem(TOKEN_KEY),
    // 放在容器中
    cachePages: ["layout"]
  },
  mutations: {
    setUser(state, data) {
      state.user = data;
      setItem(TOKEN_KEY, state.user);
    },
    // 添加缓存页面
    addCachePage(state, pageName) {
      // 没有添加，有不加
      if (!state.cachePages.includes(pageName)) {
        state.cachePages.push(pageName);
      }
    },
    // 移出缓存界面
    removeCachePage(state, pageName) {
      const index = state.cachePages.indexOf(pageName);
      if (index !== -1) {
        state.cachePages.splice(index, 1);
      }
    }
  },
  actions: {},
  getters: {}
});
